package com.ccys.cms.service.impl;

import com.ccys.cms.service.RoleService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.entity.SysRole;
import com.ccys.db.mapper.SysRoleMapper;
import com.ccys.db.mapper.SysRoleModuleMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.dto.web.base.RoleDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.web.base.RoleSelectVO;
import com.ccys.db.vo.web.base.RoleVo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.ccys.common.vo.ResponseResult.success;
import static com.ccys.common.vo.ResponseResult.update;


@Service
public class RoleServiceImpl extends BaseServiceImpl<SysRole> implements RoleService {
    @Resource
    private SysRoleModuleMapper roleModuleMapper;
    @Resource
    private SysRoleMapper dao;
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    public ResponseResult queryRoles( RoleDto roleDto) {
        if(StringUtil.isNotEmpty(roleDto.getGroupId())) {
            roleDto.setGroupId(UserUtil.getCurLoginUserGroupId());
        }
        PageDto.startPage();
        dao.setSessionGroupConcatMaxLen();
        List<RoleVo> roleVos = dao.queryRoles(roleDto);
        PageInfo<RoleVo> info = new PageInfo<>(roleVos);
        return success(info);
    }

    @Override
    @Transactional
    public ResponseResult insertRole(RoleDto roleDto) {
        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleDto,role);
        role.setStatus(1);
        saveEntity(role);
        String moduleIds = roleDto.getModuleIds();
        return update(roleModuleMapper.insertRelation(role.getId(), Arrays.asList(moduleIds.split(",")),role.getCreatedBy()));

    }

    @Override
    @Transactional
    public ResponseResult updateRole(RoleDto roleDto) {
        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleDto,role);
        String moduleIds = roleDto.getModuleIds();
        if (StringUtils.isNotBlank(moduleIds)) {
            roleModuleMapper.deleteRelation(role.getId());
            roleModuleMapper.insertRelation(role.getId(), Arrays.asList(moduleIds.split(",")), role.getCreatedBy());
        }
        if(roleDto.getStatus() != null) {
            this.baseTblAuditTrailWebService.saveStatusLog(roleDto,roleDto.getStatus());
        }
        return update(updateEntityById(role));
    }

    @Override
    public ResponseResult selectList(RoleDto roleDto) {
        List<RoleSelectVO> list = this.dao.selectList(roleDto);
        return success(list);
    }
}
